﻿using System;
using Utk.Nlp;

namespace Utk
{
    class YesNoRequest
    {
        private readonly IGenerator _request;

        public YesNoRequest(IGenerator request)
        {
            _request = request;
        }

        public static implicit operator YesNoRequest(Pattern pattern)
        {
            return new YesNoRequest(pattern);
        }

        public static implicit operator YesNoRequest(string text)
        {
            return new YesNoRequest(new Pattern(text));
        }


        public Maybe<bool> Ask()
        {
            var reply = User.Listen(_request);

            if (reply.Is(Knowledge.AffermativeResponse))
            {
                return true;
            }
            else if (reply.Is(Knowledge.NegativeResponse))
            {
                return false;
            }
            else
            {
                return Maybe<bool>.Nothing;
            }
        }

        public Maybe<T> Ask<T>(Func<T> positive, Func<T> negative)
        {
            var result = Ask();

            return result.Apply(
                _ => _ 
                         ? positive() 
                         : negative());
        }

        public Maybe<T> Ask<T>(Func<T> positive)
        {
            var result = Ask();

            return result.Apply(
                _ => _
                         ? new Maybe<T>(positive())
                         : Maybe<T>.Nothing);
        }
    }
}